# -*- coding: utf-8 -*-
"""
 @Time    : 2021/2/5 下午6:10
 @Author  : tgy
 
"""
from socket import socket, AF_INET, SOCK_STREAM
from functools import partial


class Connect:

    def __init__(self, addr, family=AF_INET, type=SOCK_STREAM):
        self.addr = addr
        self.family = family
        self.type = type
        self.socket = None

    def __enter__(self):
        if self.socket:
            raise RuntimeError('connected')
        self.socket = socket(family=self.family, type=self.type)
        self.socket.connect(self.addr)
        print(self.socket)
        return self.socket

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.socket.close()
        self.socket = None


def test01():
    ...
    addr = ('127.0.0.1', 8082)
    with Connect(addr=addr) as connect:
        ...
        connect.send(b'GET / HTTP/1.0\r\n')
        connect.send(b'\r\n')
        for content in iter(partial(connect.recv,1024), b''):
            print(content)
        # print(connect.recv(5))
        # response = b''.join(iter(partial(connect.recv,1024),b''))
        # print(response)


if __name__ == '__main__':
    ...
    test01()
